<?php
declare(strict_types=1);

namespace app\common\lib;

class Arr
{
    /**
     * 生成无限极分类树
     *
     * @param array $data
     * @return array
     */
    public static function getClassTree(array $data)
    {
        $item = $tree = [];

        foreach ($data as $val) $item[$val['id']] = ['category_id' => $val['id'], 'pid' => $val['pid'], 'name' => $val['name']];

        foreach ($item as $itek=>$itev) {
            if (isset($item[$itev['pid']])) {
                $item[$itev['pid']]['list'][] = &$item[$itek];
            } else {
                $tree[] = &$item[$itek];
            }
        }

        return $tree;
    }

    /**
     * 生成无限极栏目
     *
     * @param array $data
     * @return array
     */
    public static function getModuleTree(array $data)
    {
        $item = $tree = [];
        $urlRoot = request()->root();
        foreach ($data as $val) {
            $item[$val['id']] = [
                'pid' => $val['pid'],
                'title' => $val['name'],
                'icon'  => $val['icon'],
                'target' => '_self',
                'href'   => $val['module'] ? $urlRoot."/".implode('/', [$val['module'], $val['action']]) : ''
            ];
        };

        foreach ($item as $itek=>$itev) {
            if (isset($item[$itev['pid']])) {
                $item[$itev['pid']]['child'][] = &$item[$itek];
            } else if (!$itev['pid']) {
                $tree[] = &$item[$itek];
            }
        }
        return $tree;
    }

    /**
     * 无限极分类每级显示个数截取
     *
     * @param array $data
     * @param int $first
     * @param int $two
     * @param int $thr
     * @return array
     */
    public static function sliceClassTree(array $data, int $first = 5, int $two = 3, int $thr = 5)
    {
        $data = array_slice($data, 0, $first);
        foreach ($data as $firstkey=>&$firstval) {
            if (isset($firstval['list'])) {
                $data[$firstkey]['list'] = array_slice($firstval['list'], 0, $two);
                foreach ($firstval['list'] as $twokey=>&$twoval) {
                    if (isset($twoval['list'])) {
                        $data[$firstkey]['list'][$twokey]['list'] = array_slice($twoval['list'], 0, $thr);
                    }
                }
            }
        }
        return $data;
    }

    /**
     * 返回分页默认定义数据
     *
     * @param int $number
     * @return array
     */
    public static function getPaginateDefaultData(int $number = 10)
    {
        return [
            'total' => 0,
            'per_page' => $number,
            'current_page' => 1,
            'data' => []
        ];
    }

    /**
     * 通过数组的键进行排序
     *
     * @param array $data
     * @param string $key
     * @return array|bool
     */
    public static function arraySortByKey(array $data = [], string $key = null, int $sort = SORT_DESC)
    {
        if (!is_array($data) || !$key) return [];
        array_multisort(array_column($data, $key), $sort, $data);
        return $data;
    }
}